package org.example.user.manage.service.impl;


import org.example.commons.entity.SysHealthStatus;
import org.example.commons.entity.SysUser;
import org.example.commons.enums.HealthStatusEnum;
import org.example.commons.utils.UpdateUtil;
import org.example.user.manage.form.SysUserSearchForm;
import org.example.user.manage.repository.SysHealthStatusRepository;
import org.example.user.manage.repository.SysUserRepository;
import org.example.user.manage.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * 住户 服务层接口实现
 *
 * @author bothin
 */
@Service
public class SysUserServiceImpl implements ISysUserService {
    @Autowired
    private SysUserRepository sysUserRepository;

    @Autowired
    private SysHealthStatusRepository healthStatusRepository;

    @Override
    public SysUser queryById(Long id) {
        Optional<SysUser> sysUserOptional = sysUserRepository.findById(id);
        return sysUserOptional.orElse(null);
    }

    @Override
    public Page<SysUser> queryAllPage(Pageable pageable) {
        return sysUserRepository.findAll(pageable);
    }

    @Override
    @Transactional
    public SysUser insert(SysUser entity) {
        entity = updateInfo(entity);
        SysUser saveUser = sysUserRepository.save(entity);
        SysHealthStatus healthStatus = SysHealthStatus.builder()
                .userId(saveUser.getUserId())
                .healthStatus(HealthStatusEnum.HEALTH.getCode())
                .accessTimes(0)
                .temperature(37.0f)
                .remarks("")
                .build();
        healthStatusRepository.save(healthStatus);
        return saveUser;
    }

    @Override
    public SysUser update(SysUser entity) {
        entity = updateInfo(entity);
        return sysUserRepository.save(entity);
    }

    @Override
    public void deleteById(Long id) {
        sysUserRepository.deleteById(id);
    }

    @Override
    public List<SysUser> findByUserIdIn(Long[] ids) {
        return sysUserRepository.findByUserIdIn(ids);
    }

    @Override
    public List<SysUser> queryAll() {
        return sysUserRepository.findAll();
    }

    @Override
    public List<SysUser> queryLastUserBydays(Integer days) {
        return sysUserRepository.findLastUsers(days);
    }

    @Override
    public SysUser findByOpenid(String openid) {
        return sysUserRepository.findByOpenid(openid);
    }

    @Override
    public SysUser findByPhone(String phone) {
        return sysUserRepository.findByPhone(phone);
    }

    @Override
    public Page<SysUser> searchList(SysUserSearchForm form, Pageable pageable) {
        form.setName("%" + form.getName() + "%");
        form.setAddress("%" + form.getAddress() + "%");
        form.setHouseInfo("%" + form.getHouseInfo() + "%");
        form.setIdCard("%" + form.getIdCard() + "%");
        form.setOpenid("%" + form.getOpenid() + "%");
        form.setPhone("%" + form.getPhone() + "%");
//        System.out.println(form);
        return sysUserRepository.searchList(form, pageable);
    }

    @Override
    public String getPassword(Long id) {
        return sysUserRepository.getPassword(id);
    }

    @Override
    public void setPassword(Long id, String password) {
        sysUserRepository.setPassword(id, password);
    }

    /**
     * 补全信息 (null表示未更新，要替换成原来的数据)
     *
     * @param user 住户
     * @return
     */
    private SysUser updateInfo(SysUser user) {
        if (user == null) return null;
        if (user.getUserId() == null) return user;
        SysUser sysUser = queryById(user.getUserId());
        if (sysUser == null) return null;
        //把非空属性更新
        UpdateUtil.copyNullProperties(user, sysUser);
        return sysUser;
    }
}
